#pragma once

#include <mutex>
#include <string>
#include <vector>
#include "../include/onvif_types.h"

namespace El {
namespace Onvif {

/**
 * @brief ONVIF 服务器的最小实现
 *
 * 仅提供基础的状态管理与配置存储能力，后续功能可以在此基础上逐步扩展。
 */
class OnvifServerImpl {
public:
    explicit OnvifServerImpl(int port = 8080);

    bool Start();
    void Stop();
    bool IsRunning() const;

    void SetDeviceInformation(const DeviceInformation &deviceInfo);
    DeviceInformation GetDeviceInformation() const;

    void SetNetworkConfiguration(const NetworkInterface &networkConfig);
    NetworkInterface GetNetworkConfiguration() const;

    void SetOnvifPort(int port);
    int GetOnvifPort() const;

    void EnableWSDiscovery(bool enable);
    bool IsWSDiscoveryEnabled() const;

    void SetSecurityMode(SecurityMode mode);
    SecurityMode GetSecurityMode() const;

    bool AddUser(const User &user);
    bool RemoveUser(const std::string &username);
    std::vector<User> GetUsers() const;

    bool AddMediaProfile(const MediaProfile &profile);
    bool RemoveMediaProfile(const std::string &token);
    std::vector<MediaProfile> GetMediaProfiles() const;

    ServiceCapabilities GetServiceCapabilities() const;

private:
    static ServiceCapabilities CreateDefaultCapabilities();
    static DeviceInformation CreateDefaultDeviceInformation();
    static NetworkInterface CreateDefaultNetworkConfiguration();

private:
    mutable std::mutex stateMutex_;
    mutable std::mutex userMutex_;
    mutable std::mutex profileMutex_;

    int onvifPort_;
    bool isRunning_;
    bool wsDiscoveryEnabled_;
    SecurityMode securityMode_;

    DeviceInformation deviceInfo_;
    NetworkInterface networkConfig_;
    std::vector<User> users_;
    std::vector<MediaProfile> mediaProfiles_;
    ServiceCapabilities serviceCapabilities_;

    static constexpr std::size_t kDefaultMaxUsers = 8;
    static constexpr std::size_t kDefaultMaxProfiles = 8;
};

} // namespace Onvif
} // namespace El
